package com.tuyan.aixiang.activity.add;


import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;

import com.tuyan.aixiang.R;
import com.tuyan.aixiang.api.HttpClient;
import com.tuyan.aixiang.api.helper.ResultTransformer;
import com.tuyan.aixiang.api.helper.RxSchedulers;
import com.tuyan.aixiang.common.AppData;
import com.tuyan.aixiang.common.BaseActivity;
import com.tuyan.aixiang.common.BasePresenter;
import com.tuyan.aixiang.common.M;
import com.tuyan.aixiang.model.YsDevice;
import com.tuyan.aixiang.util.ToastUtil;
import com.tuyan.aixiang.util.YSUtil;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class AddDeviceActivity extends BaseActivity<BasePresenter> {

    YsDevice ysDevice;

    @BindView(R.id.iv_icon)
    ImageView ivIcon;

    @BindView(R.id.tv_model)
    TextView tvModel;

    @Override
    public void bindView() {
        super.bindView();
        Intent intent = getIntent();
        YsDevice device = (YsDevice) intent.getSerializableExtra("device");
        if(null == device) {
            finish();
            return;
        }
        ysDevice = device;
        if(null != ysDevice.getDefaultPicPath()) {
            loadImage(ysDevice.getDefaultPicPath(), ivIcon);
        }
        tvModel.setText(ysDevice.getModelType());
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_device;
    }

    @Override
    protected BasePresenter createPresenter() {
        return new BasePresenter();
    }

    @OnClick(R.id.btn_add)
    public void addDevice() {
        showLoading();
        Disposable disposable =  HttpClient.getDevice().addDevice(M.m()
                .put("dr_validate_code", ysDevice.getVerifyCode())
                .put("dr_device_serial", ysDevice.getDeviceSerial())
                .put("dr_device_nature", AppData.getLanuchMode())
                .put("dr_model", YSUtil.getModelType(ysDevice))
        ).compose(RxSchedulers.io_main())
                .compose(ResultTransformer.handleResult())
                .subscribe(data -> {
//                    onSuccess();
                    hideLoading();
                    ToastUtil.makeShort("设备添加成功");
                    finish();
                }, throwable -> {
                    hideLoading();
                });
        addDisposable(disposable);
    }

    private void onSuccess() {

    }

}